package com.leadership.api.job;

import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.demo2do.core.utils.DateFormatUtils;
import com.leadership.api.service.ActionLearningService;

/**
 * 
 * @author dan
 *
 */
@Component("actionLearningJob")
public class ActionLearningJob {
    
    private static final Logger logger = LoggerFactory.getLogger(ActionLearningJob.class);
    
    @Autowired
    private ActionLearningService actionLearningService;
    
    /**
     * run at 23:00 am every day
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void scanActionLearningStart() {
        
        try {
            
            logger.info("#scanActionLearningStart() - start at {}", new Date());
            
            Date day = DateUtils.addDays(new Date(), 1);
            Date startDate = com.demo2do.core.utils.DateUtils.parseDate(DateFormatUtils.format(day, "yyyy-MM-dd"), "yyyy-MM-dd");
            
            actionLearningService.startActionLearning(startDate);
            
            logger.info("#scanActionLearningStart() - finish at {}", new Date());
            
        } catch (Exception e) {
            logger.error("#scanActionLearningStart() - occur exception=[{}]", e);
        }
        
    }

}
